var form;
var table;
var layer;
var upload;
layui.use(['layer','form','table','upload'],function(){
    form = layui.form;
    table=layui.table;
    layer=layui.layer;
    upload=layui.upload;
    //课程信息
    selCourses();

});

//课程分类下拉菜单
$.ajax({
    url: "/Teacher/selCourseClassSelect",
    type: "get",
    dataType: "json",
    success: function (data) {
        for(var a=0;a<data.obj.length;a++){
            $("#class_id").append("<option value='"+data.obj[a].id+"'>"+data.obj[a].name+"</option>");
        }
    }
});

//商品数据表格
function selCourses(){
    var course_name=$("#course_name").val();
    var class_id=$("#class_id").val();
    var true_name=$("#true_name").val();
    var course_nprice=$("#course_nprice").val();
    var course_cstatus=$("#course_cstatus").val();


    $.ajax({
        url:"/Teacher/selMicroRing",
        type:"post",
        dataType:"json",
        data:{},
        success:function(data){
            console.log(data);
            table.render({
                elem: "#table_id",
                title: "表格",
                limit: 10,
                page: true,
                data:data.obj,
                cols: [[
                    {type: 'checkbox', fixed: 'left'},
                    {field: 'microcircle_id', title: '编号', width: 80},
                    {field: 'account', title: '用户名称', width: 100},
                    {field: 'article_title', title: '文章名称', width: 100},
                    {field: 'create_date', title: '创建时间', width: 160},
                    {field: 'course_no', title: '用户账号', width: 100},
                    {field: 'article_like', title: '点击数', width: 80},
                    {field: 'article_share', title: '收藏数', width: 80},
                    {field: 'article_hits', title: '查看数', width: 80},
                    {field: 'article_share', title: '分享数', width: 80},
                    {field: '', title: '排序（点赞数）', width: 115},
                    {field: '', title: '操作', width: 100, templet: function (data) {
                            return "<span id='render' onclick='javascript:check(\""+data.course_id+"\");'>查看</span>"+
                                "<span id='bianji' onclick='javascript:edit(\""+data.course_id+"\");'>审核</span>" +"<br/>";
                        }
                    }
                ]]
            });
        }
    })
}

//刷新
function refresh() {
    location.reload();
}
//关闭
function close1() {
    window.close();
}

//点击查检索页面
function retrieve() {
    //点击检索之后清空输入框
    $("input").val("");
    type: "post",
        $(this).find("option:selected").removeAttr("selected");

    layer.open({
        type : 2,//弹出层的类型
        id:1,
        title : "<div style='color:#666666;font-weight: 600;background: #F8F8F8;'>高级检索</div>",
        area : [ '670px', '500px' ],
        maxmin: true,
        shade : 0,
        offset: 'auto',
        content :"CourseRetrieve.html",
        success:function(){
        }
    });
    window.layer_1=layer;
}

//点击查看按钮弹出详情页面
function selCourses2(id){
    $.ajax({
        url: "/Teacher/selCourses",
        type:"post",
        dataType: "json",
        data:{"course_id":id},
        success: function (data) {
            selCourses2Layer(data.obj[0])
        }
    })
}
function selCourses2Layer(data){
    layer.open({
        type : 2,//弹出层的类型
        id:1,
        title : "<div style='color:#666666;font-weight: 600;background: #F8F8F8;'>课程详情</div>",
        area : [ '670px', '500px' ],
        maxmin: true,
        shade : 0,
        offset: 'auto',
        content :"CourseDetails.html",
        success:function(layero,index){
            var obj=layero.find('iframe').contents();

            obj.find('input[name=course_no]').val(data.course_no);
            obj.find('input[name=course_name]').val(data.course_name);
            obj.find('input[name=course_nprice]').val(data.course_nprice);
            obj.find('input[name=true_name]').val(data.true_name);
            obj.find('input[name=name]').val(data.name);
            obj.find('input[name=sale_num]').val(data.sale_num);
            obj.find('#course_intro').val(data.course_intro);
        }
    });
    window.layer_1=layer;
}

//点击编辑
function CourseUpdate(id){
    $.ajax({
        url: "/Teacher/selCourses",
        type: "post",
        dataType: "json",
        data:{"course_id":id},
        success: function (data) {
            ProductUpdateLayer(data.obj[0])
        }
    });
}

//弹出修改页面
function  ProductUpdateLayer(data) {
    layer.open({
        type : 2,//弹出层的类型
        id:1,
        title : "<div style='color:#666666;font-weight: 600;background: #F8F8F8;'>修改课程</div>",
        area : [ '670px', '580px' ],
        maxmin: true,
        shade : 0,
        offset: 'auto',
        content :"CourseUpdate.html",
        success:function(layero,index){
            var obj=layero.find('iframe').contents();
            obj.find('input[name=courseNo]').val(data.course_no);
            obj.find('input[name=courseName]').val(data.course_name);
            obj.find('input[name=courseNprice]').val(data.course_nprice);
            obj.find('input[name=coursePrice]').val(data.course_nprice);
            obj.find('input[name=saleNum]').val(data.sale_num);
            obj.find('#courseIntro').val(data.course_intro);
            obj.find('#courseId').val(data.course_id);
        }
    });
    window.layer_1=layer;
}

//删除课程
function delCourse(id) {
    $.ajax({
        url: "/Teacher/delCourse",
        type: "post",
        dataType: "json",
        data:{"courseId":id},
        success: function (data) {
            if(data.code==0){
                selCourses();
                layer.msg('删除成功')
            }else{
                layer.msg('删除失败')
            }
        }
    });
}

//审核详情
function ProductExamin(id) {
    $("#course_id").val(id);
    layer.open({
        type : 2,
        id:1,
        title : "<div style='color:#666666;font-weight: 600;background: #F8F8F8;'>审核详情</div>",
        area : [ '700px', '500px' ],
        maxmin: true,
        shade : 0,
        offset: 'auto',
        content :"CourseExamin.html",
        success:function(){
        }
    });
    window.layer_1=layer;
}

//查看商品日志
function selCourseLog(course_id) {
    location.href="CourseLog.html?"+course_id;
}



//课程分类
selCourseClass()
function selCourseClass() {
    $.ajax({
        url: "/Teacher/ProductCategory",
        type: "get",
        dataType: "json",
        success: function (data) {
            shouZTree(data.obj);
        }
    });
};

function shouZTree(data){
    $(function(){
        //获取值
        function zTreeOnClick(event, treeId, treeNode) {
            return false;
        };
        var ZtreeTile;
        function OnRightClick(event, treeId, treeNode) {
            if (!treeNode && event.target.tagName.toLowerCase() != "button" && $(event.target).parents("a").length == 0) {
                zTree.cancelSelectedNode();
                showRMenu("root", event.clientX, event.clientY);
            } else if (treeNode && !treeNode.noR) {
                ZtreeTile=treeNode;
                showRMenu("node", event.clientX, event.clientY);
            }
        }
        function showRMenu(type, x, y) {
            $("#rMenu ul").show();
            if (type=="root") {
                $("#m_del").hide();
                $("#m_check").hide();
                $("#m_unCheck").hide();
            } else {
                $("#m_del").show();
                $("#m_check").show();
                $("#m_unCheck").show();
            }
            y += document.body.scrollTop;
            x += document.body.scrollLeft;
            rMenu.css({"top":y+"px", "left":x+"px", "visibility":"visible"});
            $("body").bind("mousedown", onBodyMouseDown);
        }
        function hideRMenu() {
            if (rMenu) rMenu.css({"visibility": "hidden"});
            $("body").unbind("mousedown", onBodyMouseDown);
        }
        function onBodyMouseDown(event){
            if (!(event.target.id == "rMenu" || $(event.target).parents("#rMenu").length>0)) {
                rMenu.css({"visibility" : "hidden"});
            }
        }


        //添加节点
        $("#m_add").bind("click",function() {
            var parentId=ZtreeTile.id;
            layer.open({
                type:1,
                title:"添加分类",
                area:['420px','200px'],
                offset:'auto',
                shade:0.4,
                content:"<div class='layui-inline' style='margin:40px 0px 0px 40px;z-index: 10'>" +
                    "<label class='layui-form-label'>课程分类：</label>" +
                    "<div class='layui-input-block'>" +
                    "<input type='text' name='name' id='name' class='layui-input' placeholder='课程分类'/>" +
                    "</div>" +
                    "</div>",
                btn:['添加','取消'],
                yes:function(){
                    var name=$("#name").val();
                    $.ajax({
                        url:"/Teacher/addCourseClass",
                        type:"post",
                        dataType:"json",
                        data:{"name":name,"type":2,"parentId":parentId},
                        success:function(data){
                            if(data.code==0){
                                location.reload();
                            }
                        }
                    });
                }
            });
        });

        //修改节点
        $("#m_check").bind("click",function(){
            var id = ZtreeTile.id
            console.log(id)
            layer.open({
                type:1,
                id:2,
                title:"修改分类",
                area:['420px','200px'],
                offset:'auto',
                shade:0.4,
                content:"<div class='layui-inline' style='margin:40px 0px 0px 40px;z-index: 10'>" +
                    "<label class='layui-form-label'>课程分类：</label>" +
                    "<div class='layui-input-block'>" +
                    "<input type='text' name='name' id='name' class='layui-input' placeholder='课程分类'/>" +
                    "</div>" +
                    "</div>",
                btn:['修改','取消'],
                yes:function(){

                    var name=$("#name").val();
                    $.ajax({
                        url:"/Teacher/updCourseClass",
                        type:"post",
                        dataType:"json",
                        data:{"name":name,"id":id},
                        success:function(data){
                            if(data.code==0){
                                location.reload();
                            }
                        }
                    });
                }
            });
        });

        //删除节点
        $("#m_del").bind("click",function() {
            $.ajax({
                url:"/Teacher/delCourseClass",
                type:"post",
                dataType:"json",
                data:{"id":ZtreeTile.id},
                success:function(data){
                    if(data.code==0){
                        location.reload();
                    }
                }
            });
        });

        function resetTree() {
            hideRMenu();
            $.fn.zTree.init($("#treeDemo"), setting, zTreeNodes);
        }

        var zTree, rMenu;
        $(document).ready(function(){
            $.fn.zTree.init($("#treeDemo"), setting, zTreeNodes);
            zTree = $.fn.zTree.getZTreeObj("treeDemo");
            rMenu = $("#rMenu");
        });
        var setting={
            /*check: {
                enable: true,
                chkStyle: "checkbox",//显示 checkbox 选择框，默认checkbox可选择值radio
            },*/
            callback: {
                onRightClick: OnRightClick,
                onClick: zTreeOnClick,
            },
            view: {
                dblClickExpand: false,
                showIcon: false,
            },
        };
        var zTreeNodes=data;
        var city = $.fn.zTree.init($("#treeDemo"), setting, zTreeNodes);
    });
}



